package com.ruoyi.house.controller;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.house.domain.RoomInfoEntity;
import com.ruoyi.house.domain.RoomInfoSearchFormRequestBean;
import com.ruoyi.house.domain.SysRegionEntity;
import com.ruoyi.house.dto.request.PageTbRoomInfoJzRequestDTO;
import com.ruoyi.house.dto.request.PageTbRoomInfoRequestDTO;
import com.ruoyi.house.dto.response.DetailTbRoomResponseDTO;
import com.ruoyi.house.form.RoomInfoPopRequestBean;
import com.ruoyi.house.service.SysRegionService;
import com.ruoyi.house.service.TbRoomInfoService;
import com.ruoyi.house.utils.BeanUtils;
import com.ruoyi.house.utils.ModelAndViewUtil;
import com.ruoyi.house.vo.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @Title: TbRoomInfoController
 * @Description: Controller -房态
 * @date 2018年4月24日 下午6:09:19
 */
@Controller
@RequestMapping("/roomInfo")
public class TbRoomInfoController extends BaseController {

	public static final Logger log = LoggerFactory.getLogger(Logger.class);

	@Resource
	private TbRoomInfoService tbRoomInfoService;

	@Resource
	private SysRegionService sysRegionService;

	/**
	 * @Description: 页面跳转
	 * @return ModelAndView
	 */
	@RequestMapping("show")
	public ModelAndView show() {
		ModelAndView mv = ModelAndViewUtil.of("fangtai/room_info");
		String parentId = "1";
		List<SysRegionEntity> sysRegionList = sysRegionService.queryRegionList(parentId);
		mv.addObject("syslist", sysRegionList);
		return mv;
	}

	/**
	 * 列表 - 条件查询
	 */
	@RequestMapping("/list")
	public ModelAndView list(@Validated RoomInfoSearchFormRequestBean bean, HttpServletRequest request) {
		System.out.println("房态图，房源列表");
		// Integer pageNum = 1;
		// String pNum = request.getParameter("pagenum");
		// if (StringUtils.isNotBlank((pNum))) {
		// try {
		// pageNum = Integer.valueOf(pNum);
		// } catch (NumberFormatException e) {
		// }
		// } else if (StringUtils.isNotBlank((bean.getPageNum()))) {
		// pageNum = Integer.parseInt(bean.getPageNum());
		// }
		// 房产类型 0 分散式 、1集中式 hostType
		if (bean.getHostType() == 0) {
			PageTbRoomInfoRequestDTO dto = new PageTbRoomInfoRequestDTO();
			dto.setPageSize(10);
			dto.setPageNum(this.getPageNum(request));
			BeanUtils.copy(bean, dto);
			PageTbRoomInfoRequestDTO page = tbRoomInfoService.pageListTbRoom(dto);
			log.info("{}:{}>>{}", "分散式", "分页查询", "记录" + page.getReturnNum() + "条");
			log.info("{}:{}>>{}", "分散式", "分页查询", "结果集" + JSONObject.toJSONString(page.getRecords()));
			ModelAndView mv = ModelAndViewUtil.of("fangtai/search_fs_sub");
			mv.addObject("pageList", page);
			mv.addObject("pageInfo", dto.getPageInfo());
			return mv;
		} else {
			PageTbRoomInfoJzRequestDTO dto = new PageTbRoomInfoJzRequestDTO();
			dto.setPageSize(1);
			dto.setPageNum(this.getPageNum(request));
			BeanUtils.copy(bean, dto);
			PageTbRoomInfoJzRequestDTO page = tbRoomInfoService.pageListTbRoomByZj(dto);
			System.out.println("MyTotal:" + page.getReturnNum());
			ModelAndView mv = ModelAndViewUtil.of("fangtai/search_jz_sub");
			mv.addObject("pageList", page);
			log.info("{}:{}>>{}", "集中式", "分页查询", "记录" + page.getReturnNum() + "条");
			mv.addObject("pageInfo", dto.getPageInfo());
			log.info("{}:{}>>{}", "集中式", "分页查询", "记录：" + JSONObject.toJSON(page.getRecords()));
			System.out.println("MyTotal:" + page.getReturnNum());
			return mv;
		}
	}

	/**
	 * 
	 * @Description: 房态 - 弹层
	 * @return ModelAndView
	 */
	@RequestMapping("pop")
	@ResponseBody
	public ModelAndView pop(RoomInfoPopRequestBean requestBean) {
		ModelAndView mv = ModelAndViewUtil.of("fangtai/room_info_pop");
		DetailTbRoomResponseDTO room = tbRoomInfoService.getRoomInfoDetailById(requestBean.getRoomId(),
				requestBean.getBeforeCfContractId());
		room.setBeforeCfContractId(requestBean.getBeforeCfContractId());
		room.setClassName(requestBean.getClassName());
		room.setLeisureDate(requestBean.getLeisureDate());
		room.setPreName(requestBean.getPreName());
		room.setHouseId(requestBean.getHouseId());
		room.setCheckRegister(requestBean.getCheckRegister());
		room.setCheckTerm(requestBean.getCheckTerm());
		room.setTop(requestBean.getTop());
		room.setLeft(requestBean.getLeft());
		room.setRight(requestBean.getRight());
		room.setEast(requestBean.getEast());
		mv.addObject("popData", room);
		return mv;
	}

	/**
	 * @Description: 房态 - 修改状态
	 * @param requestBean
	 * @return ModelAndView
	 */
	@RequestMapping("update_status")
	@ResponseBody
	public R updateStatus(RoomInfoPopRequestBean requestBean) {
		RoomInfoEntity entity = tbRoomInfoService.getRoomInfoById(requestBean.getRoomId());
		Date nowDate = new Date();
		int roomStatus = requestBean.getRoomStatus();
		if (roomStatus == 0) {
			entity.setVacancyDate(nowDate);
		} else if (roomStatus == 2) {
			entity.setFinishDate(nowDate);
		}
		entity.setStatus(requestBean.getRoomStatus());
		tbRoomInfoService.UpdateTbRoomInfo(entity);
		return R.ok();
	}

	/**
	 * @Description: 房态 - 修改金额
	 * @param requestBean
	 * @return ModelAndView
	 */
	@RequestMapping("update_rental")
	@ResponseBody
	public R updateRental(RoomInfoPopRequestBean requestBean) {
		RoomInfoEntity entity = tbRoomInfoService.getRoomInfoById(requestBean.getRoomId());
		entity.setRental(requestBean.getRental());
		tbRoomInfoService.UpdateTbRoomInfo(entity);
		return R.ok();
	}
}
